package com.example.demo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.yaml.snakeyaml.Yaml;

import com.example.demo.model.Books;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

@SpringBootApplication
public class SpringDataDemoApplication {

	@Resource
	private Yaml yaml;
	
	public static void main(String[] args) {
		SpringApplication.run(SpringDataDemoApplication.class, args);
	}

	@Bean
	public ObjectMapper objectMapper() {
		return new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
	}
	
	@Bean
	public Yaml yaml() {
		return new Yaml();
	}
	@Bean
	public List<Books> books(){
		Map<String,Object> load = yaml.load(getClass().getClassLoader().getResourceAsStream("books.yml"));
		List<Map<String,Object>> object = (List<Map<String, Object>>) load.get("books");
		List<Books>  list = new ArrayList<>();
		object.forEach(map->{
			Books books = new Books();
			books.setBookName(map.get("name").toString());
			books.setAuthor(map.get("author").toString());
			books.setPrice((Double)map.get("price"));
			list.add(books);
		});
		return list;
	}

}
